import axios from "axios"

const instance = axios.create({
  timeout: 20000
})

instance.interceptors.request.use(
  config => {
    const token = localStorage.getItem("token")
    if (token) {
      config.headers.Authorization = token
    }
    return config
  },
  error => {
    return Promise.reject(error)
  }
)

const network = (method, url, data, requestType) => {
  let requestData = ["get"].includes(method.toLowerCase()) ? {
    params: data
  } : {
    data
  }
  return new Promise(resolve => {
    instance({
      url,
      method,
      ...requestData,
      requestType
    }).then(response => {
      resolve(response)
    }).catch(error => {
      // resolve(error)// 胡壮强，注释原因是，当请求不到数据后，界面上会弹出错误窗口。无法演示项目
    })
  })
}
export default network